<?php
/**
* 
* 
*/
    class webim {
        //webim服务端的参数信息
        private $socket_host;   //连接地址
        private $socket_port;   //端口
        private $socket_action; //动作

        //webim api接口
        private $api_host;
        private $api_port;
        private $api_sid;

        //用户信息
        private $account;
        //用户密码
        private $password;

        /**
        * 构造函数
        * 
        * @param string $socket_host tcp服务器地址
        * @param int $socket_port    tcp服务器端口
        * @param string $app_host    app服务器地址
        * @param int $app_port       app服务器端口
        * @param string $app_sid     app服务器sid文件路径
        * @return void
        */
        function  __construct($socket_host , $socket_port , $app_host , $app_port , $app_sid) {
            if($socket_host == '' || $socket_port == '' || $app_host =='' || $app_port =='' || $app_sid == ''){
                exit('class webim __construct exit.');  
            }
            $this->socket_host = $socket_host;
            $this->socket_port = $socket_port;
            $this->api_host = $app_host;
            $this->api_port = $app_port;
            $this->api_sid = $app_sid;
        }

        /*析构函数*/
        public function __destruct() {

        }

        //设置用户名和密码
        public function set_account($account , $md5_password){
            $this->account = $account;
            $this->password = $md5_password;
        }




        //不需要登录身份的
        //-----------------------------------------------------------------------------------

        public function get_remote_city($ip = ''){
            $this->socket_action = 'action_get_city';
            $result = $this->webim_socket('ip=' . urlencode($ip));
            return $this->get_json_decode($result);
        }


        /**
        * 获取用户信息
        *
        * @param string $account   用户账户
        */
        public function get_user_info($account){
            $this->socket_action = 'action_get_userinfo';
            $result = $this->webim_socket('t=' . urlencode($account));
            return $this->get_json_decode($result);
        }

        /**
        * 通过WEBIM进行LOAD加载USER到内存中
        *
        * @param string $account       账户
        * @return string
        */
        public function load_in_server($account) {
            $this->socket_action= 'action_get_loaduser';
            $result = $this->webim_socket('u=' . urlencode($account));
            return $this->get_json_decode($result);
        }


        /**
        * 通过WEBIM进行注册
        *
        * @param string $paramsArray       注册
        * @return string
        */
        public function regster($paramsArray) {
            if(!is_array($paramsArray)){
                return false;
            }
            $count = count($paramsArray);
            if( $count == 0 || $count >7){
                return false;
            }
            $keyArray = array('i', 'nick', 'c', 'x', 'p','u','md5');
            //如果传递的参数不是在范围内，就OUT
            foreach($paramsArray as $key=>$item){
                if(!in_array($key, $keyArray)){
                    return false;
                }
                $param .= '&'.$key.'='.urlencode($item);  
            }
            $this->socket_action = 'action_get_regster';
            $result = $this->webim_socket($param);
            return $this->get_json_decode($result);
        }
        
        /**
        * 同步webim的账户
        *
        * @param string $paramsArray       注册
        * @return string
        */
        public function syn_account($paramsArray){
            if(!is_array($paramsArray) || empty($paramsArray)){
                return false;
            }
            $count = 0;
            $keyArray = array('i', 'nick', 'c', 'x', 'p','u','md5','sign');
            //如果传递的参数不是在范围内，就OUT
            foreach($paramsArray as $key=>$item){
                if(!in_array($key, $keyArray) || is_null_str($item)){
                    continue;
                }
                $param .= '&'.$key.'='.urlencode($item);
                $count++; 
            }
            if($count == 0){
                return false;
            }
            $this->socket_action = 'action_get_syn';
            $result = $this->webim_socket($param);
            return $this->get_json_decode($result);
        }
        

        //---------------------------------------------------------------------------------


        /**
        * 获取session_id
        *
        * @param string $account           账户
        * @param string $password          密码（MD5加密）
        * @param string $webs              WEBIM的ID
        * @return string
        */
        public function get_session_id($account, $md5_password) {
            $this->socket_action = 'action_get_usersid';
            $result = $this->webim_socket('u=' . $account . '&p=' . $md5_password);
            //$result = $this->webim_socket('u=' . $account . '&p=' . $md5_password, $this->api_host , $this->api_port , $this->api_sid);
            $ary = $this->get_json_decode($result);
            return $ary[0];
        }


        /**
        * ping WEBIM通信
        *
        * @return string
        */
        public function ping() {
            $webs = $this->get_session_id($this->account, $this->password);
            $this->socket_action = 'action_get_ping';
            $result = $this->webim_socket('u=' . $this->account . '&s=' . $webs);
            return $this->get_json_decode($result);
        }



        /**
        * 更新用户信息             头像，昵称，城市，性别，密码
        *
        * @param array     $paramsArray     相应参数数组
        *
        */
        public function modify_user_info($paramsArray){
            if(!is_array($paramsArray)){
                return false;
            }
            $count = count($paramsArray);
            if( $count == 0 || $count >5){
                return false;
            }

            $webs = $this->get_session_id($this->account, $this->password);
            if(!$webs){
                return false;
            }
            $keyArray = array('i', 'nick', 'c', 'x', 'p');
            $param = 'u=' . $this->account . '&s=' . $webs;
            //如果传递的参数不是在范围内，就OUT
            foreach($paramsArray as $key=>$item){
                if(!in_array($key, $keyArray)){
                    return false;
                }
                $param .= '&'.$key.'='.urlencode($item);
            }            
            $this->socket_action = 'action_get_modifyuser';
            $result = $this->webim_socket($param);
            return $this->get_json_decode($result);
        }  
        

        /**
        * 城市，昵称，账户，搜索
        *
        * @param string $kw        关键字
        * @return array
        */
        public function search_friend($kw) {
            $webs = $this->get_session_id($this->account, $this->password);
            $this->socket_action = 'action_get_search_user';
            $result = $this->webim_socket('u=' . $this->account . '&s=' . $webs . '&k=' . urlencode($kw));
            return $this->get_json_decode($result);
        }

        /**
        * 发送消息，WEBIM
        *
        * @param string $msg               发送信息
        * @param string $city              城市
        * @param string $friend            好友账户
        * @param int $type                 类型        1：单用户聊天     2：公共聊天   3：群发信息
        */
        public function send_message($msg, $city, $friend, $type = 2){
            $webs = $this->get_session_id($this->account, $this->password);
            $this->socket_action = 'action_get_sendmsg';
            $sparam = '';           //URL参数
            if($type == 1){
                $sparam = $friend;
            } elseif($type == 2){
                $sparam = '[cry_msg]';
            } else {
                $sparam = '[bro_msg]';
            }
            $result = $this->webim_socket('u=' . $this->account . '&s=' . $webs . '&m=' . urlencode($msg) . '&c=' . urlencode($city) . '&t=' . $sparam);        
            return $this->get_json_decode($result);
        }


        /**
        * 验证是否是自己好友
        *
        * @param string $friend      好友账户
        *
        * return array 0 不是， 1是
        */
        public function is_friend($friend){
            $webs = $this->get_session_id($this->account, $this->password);
            $this->socket_action = 'action_get_isfriend';
            $result = $this->webim_socket('u=' . $this->account . '&t=' . urlencode($friend) . '&s=' . $webs);
            return $this->get_json_decode($result);
        }

        /**
        * 通过WEBIM获取好友列表
        *
        * @return array
        */
        public function get_friend_list() {
            $webs = $this->get_session_id($this->account, $this->password);
            $this->socket_action = 'action_get_friends';
            $result = $this->webim_socket('u=' . $this->account . '&s=' . $webs);
            return $this->get_json_decode($result);
        }

        /**
        * 添加好友
        *
        * @param string $friend                    好友账户
        * @param string $friend_nickname           好友昵称
        * @param string $friend_group              分组名称
        * @return array
        */
        public function add_friend($friend, $friend_nickname, $friend_group) {
            $webs = $this->get_session_id($this->account, $this->password);
            $this->socket_action = 'action_get_addfriend';
            $result = $this->webim_socket('u=' . $this->account . '&s=' . $webs . '&t=' . urlencode($friend) . '&a=' . urlencode($friend_nickname) . '&g=' . urlencode($friend_group));
            return $this->get_json_decode($result);
        }

        /**
        * 删除好友
        *
        * @param  string   $account   账户
        * @param  string   $friend    好友账户
        * @return josn
        */
        public function del_friend($friend) {
            $webs = $this->get_session_id($this->account, $this->password);
            $this->socket_action = 'action_get_delfriend';
            $result = $this->webim_socket('u=' . $this->account . '&s=' . $webs . '&t=' . urlencode($friend)); 
            return $this->get_json_decode($result);
        }

        /**
        * 修改好友备注，分组
        *
        * @param string $remark            备注信息
        * @param string $friend            好友账户
        * @param string $group             分组信息
        */
        public function modify_friend($friend, $remark, $group){
            $webs = $this->get_session_id($this->account, $this->password);
            $this->socket_action = 'action_get_upfriend';
            $result = $this->webim_socket('u=' . $this->account . '&s=' . $webs . '&t=' . urlencode($friend) . '&a=' . urlencode($remark) . '&g=' . urlencode($group));
            return $this->get_json_decode($result);
        }


        /**
        * 通过WEBIM获创建好友分组
        *
        * @param string $groupname     分组名称
        * @return array
        */
        public function create_friend_group($group) {
            $webs = $this->get_session_id($this->account, $this->password);
            $this->socket_action = 'action_get_friendgroup_add';
            $result = $this->webim_socket('u=' . $this->account . '&s=' . $webs . '&g=' . urlencode($group));
            return $this->get_json_decode($result);
        }

        /**
        * 删除分组
        *
        * @param string $group     分组名称
        * @return json
        */
        public function dele_group($group) {
            $webs = $this->get_session_id($this->account, $this->password);
            $this->socket_action = 'action_get_friendgroup_del';
            $result = $this->webim_socket('u=' . $this->account . '&s=' . $webs . '&g=' . urlencode($group));
            return $this->get_json_decode($result);
        }

        /**
        * 修改分组名称
        *
        * @param string $account       账户
        * @param string $webs          WEBIM的ID
        * @param string $oldgroup      旧分组名称
        * @param string $newgroup      新分组名称
        * @return json
        */
        public function update_group($oldgroup, $newgroup) {
            $webs = $this->get_session_id($this->account, $this->password);
            $this->socket_action = 'action_get_friendgroup_up';
            $result = $this->webim_socket('u=' . $this->account . '&s=' . $webs . '&g=' . urlencode($oldgroup) . '&r=' . urlencode($newgroup));
            return $this->get_json_decode($result);
        }

        /**
        * json
        *
        * @param string $result     
        * @return json
        */
        private function get_json_decode($result){
            $result = str_replace("];","]",$result);
            $result = trim(str_replace("var info = ","",$result));
            $result = str_replace("\t","",$result);
            $result = str_replace("\r","",$result);
            $result = str_replace("\n","",$result);
            //去除bom头
            $result = preg_replace( '/^(\xef\xbb\xbf)/', '', $result);
            $result = preg_replace( '/^(\xef\xbb\xbf)/', '', $result);
            return json_decode($result);
        }

        /**
        * WEBIM->Socket连接
        *
        * @param string $parament  传递参数
        * @param string $host      通许主机
        * @param string $port      通讯端口
        * @param string $action    动作
        * @return string
        */
        private function webim_socket($parament , $host = '' , $port = '' , $action = '') {
            if($host == ''){
                $host = $this->socket_host;
            }
            if($port == ''){
                $port = $this->socket_port;
            }
            if($action == ''){
                $action = $this->socket_action;
            }
            $url = 'http://'.$host.':'.$port.'/'.$action.'?'. $parament;
            
            if(function_exists(curl_init)){
               $result =  http_curl($url);  
            }else{
               $result =  dfopen($url); 
            }
            return $result;
        }
      


    }

?>
